import ConfirmModal from '@/components/ConfirmModal';
import PlaceHolderCpn from '@/components/PlaceHolderCpn';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import type { ProColumns } from '@ant-design/pro-components';
import { ProTable } from '@ant-design/pro-components';
import { useModel } from 'umi';
import { querySysRecords, removeSysRecords } from './service';
import type { SysRecordTableListItem } from './types';

export default () => {
  const { initialState } = useModel('@@initialState');
  const currUser = initialState?.currentUser;
  const auths = currUser?.auth ? new Set(currUser.auth.sysRecord || []) : new Set();
  const ifDelete = auths.has('delete');
  const columns: ProColumns<SysRecordTableListItem>[] = [
    {
      title: 'id',
      hideInTable: true,
      hideInSearch: true,
      dataIndex: '_id',
    },
    {
      title: 'userId',
      hideInTable: true,
      hideInSearch: true,
      dataIndex: 'userId',
    },
    {
      title: '用户名称',
      align: 'center',
      width: 120,
      dataIndex: 'userName',
    },
    {
      title: '用户账号',
      align: 'center',
      width: 120,
      dataIndex: 'account',
    },
    {
      title: '用户归属',
      width: 120,
      hideInSearch: true,
      align: 'center',
      dataIndex: 'ownerId',
      render: (_, record) => record.ownerId || '系统',
    },
    {
      title: 'IP地址',
      width: 150,
      hideInSearch: true,
      align: 'center',
      dataIndex: 'ip',
    },
    {
      title: '操作类型',
      width: 100,
      align: 'center',
      dataIndex: 'operType',
      valueEnum: {
        create: { text: '添加' },
        delete: { text: '删除' },
        modify: { text: '修改' },
        query: { text: '查询' },
        login: { text: '登录' },
        export: { text: '导出' },
        import: { text: '导入' },
        mobile_login: { text: '登录(移动端)' },
        mobile_query: { text: '查询(移动端)' },
        mobile_add: { text: '添加(移动端)' },
        mobile_remove: { text: '删除(移动端)' },
      },
    },
    {
      title: '目标类型',
      width: 100,
      hideInSearch: true,
      align: 'center',
      dataIndex: 'targetType',
    },
    {
      title: '操作时间',
      width: 200,
      align: 'center',
      dataIndex: 'operTime',
      valueType: 'dateTime',
      hideInSearch: true,
    },
    {
      title: '操作描述',
      width: 280,
      hideInSearch: true,
      ellipsis: true,
      dataIndex: 'description',
    },
    {
      title: '操作',
      width: 100,
      align: 'center',
      key: 'option',
      fixed: 'right',
      valueType: 'option',
      render: (_, record, x, action) =>
        ifDelete
          ? [
              <ConfirmModal
                reload={() => action?.reload()}
                operFunc={async () => await removeSysRecords(record._id)}
              />,
            ]
          : [<PlaceHolderCpn />],
    },
  ];
  return (
    <PageHeaderWrapper
      header={{
        title: '',
        subTitle: '系统日志列表',
        breadcrumb: {
          routes: [
            { path: '/', breadcrumbName: '首页' },
            {
              path: '/sysrecord-list',
              breadcrumbName: '系统日志',
            },
          ],
        },
      }}
    >
      <ProTable<SysRecordTableListItem>
        rowKey="_id"
        scroll={{ x: true }}
        style={{ whiteSpace: 'nowrap' }}
        columns={columns}
        columnEmptyText="暂无数据"
        debounceTime={300}
        request={async (param, sort) => {
          const res = await querySysRecords(param, sort);
          return res;
        }}
        dateFormatter="string"
        pagination={{
          showSizeChanger: true,
          showQuickJumper: true,
          defaultPageSize: 10,
          size: 'default',
        }}
      />
    </PageHeaderWrapper>
  );
};
